package com.chen.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;

/**
 * @decription
 * @atuhor:陈红卫
 * @create : 2017-06-19-16:10
 */
@Component
public class ThrowExceptionFilter extends ZuulFilter {
    private static Logger log= LoggerFactory.getLogger(ThrowExceptionFilter.class);


    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {

        RequestContext ctx = RequestContext.getCurrentContext();

        log.info("this is a pre filter .it will throw a RuntimeException");
        try{
            doSomethings();

        }catch (Exception e)
        {

            ctx.set("error.status_code", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            ctx.set("error.exception",e);
           // ctx.set("message","自定义异常过滤器");
        }
        return null;
    }

    private void doSomethings(){
        throw  new RuntimeException("Exist some errors");
    }
}
